package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.FlowShipMapper;
import com.yxh.dao.PositionMapper;
import com.yxh.dao.QuestionMapper;
import com.yxh.dao.ViciousMapper;
import com.yxh.pojo.*;
import com.yxh.service.*;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import com.yxh.util.TaskCode;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/6/12.
 */

@Service
@Transactional
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private ViciousMapper viciousMapper;

    @Autowired
    private FlowShipMapper flowShipMapper;


    @Autowired
    private MakingsService makingsService;

    @Autowired
    private ChangeService changeService;

    @Autowired
    private HideService hideService;

    @Autowired
    private ViciousService viciousService;

    @Autowired
    private FlowManageService flowManageService;

    @Autowired
    private TaskService taskService;


    @Override
    public int save(Position record) {
        try {
            int i=0;
            String imgs=record.getImgId(),autId=record.getAuditingId();
            if(StringUtil.isNotEmpty(imgs)){
                String id=SnowflakeIdWorker.creatId();
                String [] img=imgs.split(",");
                Map<String,Object> parm=new HashMap<String,Object>();
                parm.put("foreign_id",id);
                for(int le=0;le<img.length;le++){
                    parm.put("img_id", SnowflakeIdWorker.creatId());
                    parm.put("url",img[le]);
                    i= viciousMapper.insertImg(parm);
                }

                record.setImgId(id);
            }
            if(StringUtil.isNotEmpty(autId)){
                String aid=SnowflakeIdWorker.creatId();
                FlowShip flowShip=new FlowShip();

                flowShip.setId(aid);
                for(String u:autId.split(",")){
                    flowShip.setUserId(u);
                    i= flowShipMapper.insertShip(flowShip);
                }

                record.setAuditingId(aid);
            }

            return positionMapper.insertSelective(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int update(Position record) {

        try {
            return positionMapper.updateByPrimaryKeySelective(record);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int delete(String id) {
        return 0;
    }

    @Override
    public void batchDelete(List<Position> records) {

    }

    @Override
    public Position get(String id) {
        return null;
    }

    @Override
    public Position get(Position record) {
        return null;
    }

    @Override
    public List<Position> getList(Position record) {
        return null;
    }

    @Override
    public List<Position> getAllList(Position record) {
        return null;
    }

    @Override
    public List<Position> getListPage(PageEntity<Position> record) {
        return null;
    }

    @Override
    public PageInfo getInformation(Position record, Integer page, Integer pageSize) {
        return null;
    }

    @Override
    public int insertQ(String id, List<Map> questions) {
        try {
        int i=0;
        for(Map q:questions){
            Question p=new Question();
            p.setPositionId(id);

            String imgs= (String) q.get("imgsId");
            p.setOpinion((String) q.get("opinion"));
            p.setQuestionDescribe((String) q.get("questionDescribe"));
            p.setQuestionType((String) q.get("questionType"));
            p.setViciousLevel((String) q.get("viciousLevel"));
            if(StringUtil.isNotEmpty(imgs)){
                String newid=SnowflakeIdWorker.creatId();
                String [] img=imgs.split(",");
                Map<String,Object> parm=new HashMap<String,Object>();
                parm.put("foreign_id",newid);
                for(int le=0;le<img.length;le++){
                    parm.put("img_id", SnowflakeIdWorker.creatId());
                    parm.put("url",img[le]);
                    i= viciousMapper.insertImg(parm);
                }
                p.setImgsId(newid);
            }
          i=questionMapper.insert(p);
        }
        return i;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo getList(Map<String,Object> deptJson, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Map<String,Object>> menuList =positionMapper.selectWroks(deptJson);
        PageInfo pageInfo = new PageInfo(menuList);
        pageInfo.setList(menuList);
        return pageInfo;
    }

    @Override
    public Map<String, Object> getByPositionId(String id) {
        Map<String, Object>  Position=positionMapper.selectByPrimary(id);
        List<Map<String, Object>> questions=questionMapper.selectAll(id);
        List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
        if(Position==null){
            return Position;
        }
        if(Position.get("imgId")!=null){
            Position.put("imgs",viciousMapper.getListImgs(Position.get("imgId").toString()));
        }

        if(Position.get("auditingId")!=null){
            List<Map<String,Object>> names= flowShipMapper.selectName(Position.get("auditingId").toString());

           String name ="";
            System.out.println("bbb"+names.get(0));
            if(names.get(0)!=null){
                for(Map<String,Object> map:names){
                    name+= map.get("username").toString()+',';
                }
                Position.put("auditingName",name.substring(0, name.length()-1));
            }
           else
                Position.put("auditingName",name);

        }
        if(questions.size()!=0){
            for(Map<String, Object> q:questions){

                if(q.get("imgs_id")!=null){
                    q.put("imgs",viciousMapper.getListImgs(q.get("imgs_id").toString()));
                    q.put("imgsId",q.get("imgs_id"));
                }
                list.add(q);
            }
        }

        Position.put("question",list);
        return Position;
    }

    @Override
    public PageInfo getAuditListApp(String userId, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Map<String,Object>> tasks = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> menuList =positionMapper.selectAudits(userId);
       for(Map<String,Object> map:menuList){
           String process=map.get("keyss").toString(),taskId=map.get("id").toString();
           Map<String,Object> m=null;
           if(process.contains(TaskCode.MAKE_KEY.getDesc())){
               m=makingsService.getById((String) taskService.getVariable(taskId, "makingsId"));
               map.put("key",TaskCode.MAKE_KEY.getDesc());
           }
           else if(process.contains(TaskCode.CHANGE_KEY.getDesc())){
               m=changeService.getNewById((String) taskService.getVariable(taskId, "changeId"));
               map.put("key",TaskCode.CHANGE_KEY.getDesc());
           }
           else if(process.contains(TaskCode.HIDE_KEY.getDesc())){
               m=hideService.getNewById((String) taskService.getVariable(taskId, "hideId"));
               map.put("key",TaskCode.HIDE_KEY.getDesc());
           }
           else if(process.contains(TaskCode.FAULT_KEY.getDesc())){
               m=viciousService.get1((String) taskService.getVariable(taskId, "viciousId"));

               map.put("key",TaskCode.FAULT_KEY.getDesc());
           }
           else if(process.contains(TaskCode.SUPER_KEY.getDesc())){
               m=viciousService.get1((String) taskService.getVariable(taskId, "viciousId"));
               map.put("key",TaskCode.SUPER_KEY.getDesc());
           }
           else{
              m= getByPositionId(taskId);
               map.put("key",process);
           }
           map.put("model",m);
           tasks.add(map);
       }


        PageInfo pageInfo = new PageInfo(tasks);
        pageInfo.setList(tasks);
        return pageInfo;
    }

    @Override
    public PageInfo getListApp(String code, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Map<String,Object> ma=new HashMap<String, Object>();
        ma.put("code",code);
        List<Map<String,Object>> menuList =positionMapper.selectApp(ma);
        List<Map<String,Object>> menuLists =new ArrayList<Map<String,Object>>();
        for(Map<String,Object> m:menuList){
            m.put("model",getByPositionId(m.get("id").toString()));
            m.put("key",m.get("keyss"));
            menuLists.add(m);

        }

        PageInfo pageInfo = new PageInfo(menuLists);
        pageInfo.setList(menuLists);
        return pageInfo;


    }
}
